package com.laoda.ikun_food.controller.web.rest;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoda.ikun_food.entity.Order;
import com.laoda.ikun_food.service.OrderService;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@AllArgsConstructor
@RequestMapping("/api/user")
public class UserOrderRestController {
    private final OrderService orderService;

    @GetMapping("/userorderList")
    public IPage<Order> showAllOrder(
            @RequestParam(name="foodid",defaultValue = "", required = false) String foodid,
            @RequestParam(name="lowPrice",defaultValue = "0", required = false) BigDecimal lowPrice,
            @RequestParam(name="hiPrice",defaultValue = "0", required = false) BigDecimal hiPrice,
            @RequestParam(name = "page", defaultValue = "1", required = false) int page,
            @RequestParam(name = "size", defaultValue = "10", required = false) int size
    ){
        IPage<Order> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(size);

        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if(lowPrice != null && !lowPrice.equals(BigDecimal.ZERO)){
            wrapper.ge("price", lowPrice);
        }
        if(hiPrice != null && !hiPrice.equals(BigDecimal.ZERO)){
            wrapper.le("price", hiPrice);
        }
        if(foodid != null && !foodid.isEmpty()){
            wrapper.like("foodid", foodid);
        }
        return orderService.page(pager, wrapper);
    }

}
